package cn.com.lms.business.organization.web;

import cn.com.lms.business.core.constant.ErrorEnum;
import cn.com.lms.business.core.util.SystemUserUtil;
import cn.com.lms.business.organization.service.BasSupplierUserService;
import cn.com.lms.business.user.persistence.entity.SysUserVo;
import com.bnzj.cloud.business.core.persistence.entity.SysUser;
import com.bnzj.cloud.business.core.service.SysUserService;

import com.bnzj.core.exception.BusinessException;
import com.bnzj.core.persistence.query.CompareExpression;
import com.bnzj.core.persistence.query.CompareType;
import com.bnzj.core.persistence.query.IExpression;

import com.bnzj.core.persistence.query.QueryHelpBean;
import com.bnzj.core.persistence.util.EntityUtils;
import com.bnzj.core.rest.ResponseResult;
import com.bnzj.core.rest.ResultCode;
import com.bnzj.core.webmvc.util.WebMvcUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

@RestController
@RequestMapping(value = "/organization/supplier")
public class BasSupplierUserController {
	@Resource
	private BasSupplierUserService basSupplierUserService;
	
	@Autowired
	private SysUserService sysUserService;

	/**
	 * 
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询", parameters = {
	 		@Parameter(name = "name", description = "供应商名", allowEmptyValue = true, 
    		        schema = @Schema(implementation = String.class)),
	 		@Parameter(name = "cname", description = "供应商账户名称", allowEmptyValue = true, 
	               schema = @Schema(implementation = String.class)),
	 		@Parameter(name = "enabled", description = "1启用0禁用", allowEmptyValue = true, 
	               schema = @Schema(implementation = String.class)),})
	@GetMapping(value = "/getList")
	public ResponseResult<Page<SysUser>> getEmployeeList(HttpServletRequest request) {
		Pageable pageable = WebMvcUtils.initPage(request, Sort.by(Sort.Direction.DESC, "d.create_time"));
		String name = request.getParameter("name");
		String cname = request.getParameter("cname");
		QueryHelpBean queryHelpBean = BasSupplierUserService.getQueryHelpBean();
		queryHelpBean.setPageable(pageable);
		IExpression nameIExpression  = CompareExpression.build("e.name", CompareType.like, name);
		IExpression cnameIExpression = CompareExpression.build("d.cname", CompareType.like, cname);
		IExpression enabledIExpression = CompareExpression.build("d.enabled", CompareType.equal, request.getParameter("enabled"));
		queryHelpBean.addWhere(nameIExpression.or(cnameIExpression),enabledIExpression);
		Page<SysUser> list = basSupplierUserService.findAll(queryHelpBean);
		return ResponseResult.success(list);
	}

	@Operation(summary = "新增")
	@PostMapping(value = "/add")
	public ResponseResult<SysUser> insertCommonBasSupplier(@RequestBody SysUser sysUser) {
		 SysUserVo currentUser = SystemUserUtil.getCurrentUser();
		EntityUtils.saveBefore(sysUser, currentUser.getAccount(), currentUser.getCname());
		sysUser.setType("B");
		sysUser = sysUserService.save(sysUser);
		return ResponseResult.success(sysUser);
	}

	@Operation(summary = "根据id查询")
	@GetMapping("/{id}")
	public ResponseResult<SysUser> getById(
			@Parameter(name = "id", description = "主键", required = true) @PathVariable("id") Long id) {
		Optional<SysUser> basSupplierUser = basSupplierUserService.findBasSupplierUserById(id);
		if (!basSupplierUser.isPresent()) {
			throw new BusinessException(ErrorEnum.RECORD_NOT_EXIST.getMessage(),
					"" + ErrorEnum.RECORD_NOT_EXIST.getCode());
		}
		return ResponseResult.success(basSupplierUser.get());
	}

	@Operation(summary = "修改")
	@PostMapping(value = "/edit")
	public ResponseResult<SysUser> editCommonBasSupplier(@RequestBody SysUser sysUser) {
		if (ObjectUtils.isEmpty(sysUser.getId())){
			return ResponseResult.failure(ResultCode.FAILURE, sysUser);
		}
		sysUser = sysUserService.save(sysUser);
		return ResponseResult.success(sysUser);
	}
}
